package com.cabrida.livestar.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.StreamCorruptedException;
import java.util.List;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Base64;

/**
 * 
 * @Use:Android 专用 SharedPreferences工具
 */
public class SharedUtils {

	public static String PREFERENCE_NAME = "livestar";

	public static boolean putString(Context context, String key, String value) {
		SharedPreferences settings = context.getSharedPreferences(
				PREFERENCE_NAME, Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = settings.edit();
		editor.putString(key, value);
		return editor.commit();
	}

	public static String getString(Context context, String key) {
		return getString(context, key, null);
	}

	public static String getString(Context context, String key,
			String defaultValue) {
		SharedPreferences settings = context.getSharedPreferences(
				PREFERENCE_NAME, Context.MODE_PRIVATE);
		return settings.getString(key, defaultValue);
	}

	public static boolean putInt(Context context, String key, int value) {
		SharedPreferences settings = context.getSharedPreferences(
				PREFERENCE_NAME, Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = settings.edit();
		editor.putInt(key, value);
		return editor.commit();
	}

	public static int getInt(Context context, String key) {
		return getInt(context, key, -1);
	}

	public static int getInt(Context context, String key, int defaultValue) {
		SharedPreferences settings = context.getSharedPreferences(
				PREFERENCE_NAME, Context.MODE_PRIVATE);
		return settings.getInt(key, defaultValue);
	}

	public static boolean putLong(Context context, String key, long value) {
		SharedPreferences settings = context.getSharedPreferences(
				PREFERENCE_NAME, Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = settings.edit();
		editor.putLong(key, value);
		return editor.commit();
	}

	public static long getLong(Context context, String key) {
		return getLong(context, key, -1);
	}

	public static long getLong(Context context, String key, long defaultValue) {
		SharedPreferences settings = context.getSharedPreferences(
				PREFERENCE_NAME, Context.MODE_PRIVATE);
		return settings.getLong(key, defaultValue);
	}

	public static boolean putFloat(Context context, String key, float value) {
		SharedPreferences settings = context.getSharedPreferences(
				PREFERENCE_NAME, Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = settings.edit();
		editor.putFloat(key, value);
		return editor.commit();
	}

	public static float getFloat(Context context, String key) {
		return getFloat(context, key, -1);
	}

	public static float getFloat(Context context, String key, float defaultValue) {
		SharedPreferences settings = context.getSharedPreferences(
				PREFERENCE_NAME, Context.MODE_PRIVATE);
		return settings.getFloat(key, defaultValue);
	}

	public static boolean putBoolean(Context context, String key, boolean value) {
		SharedPreferences settings = context.getSharedPreferences(
				PREFERENCE_NAME, Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = settings.edit();
		editor.putBoolean(key, value);
		return editor.commit();
	}

	public static boolean getBoolean(Context context, String key) {
		return getBoolean(context, key, false);
	}

	public static boolean getBoolean(Context context, String key,
			boolean defaultValue) {
		SharedPreferences settings = context.getSharedPreferences(
				PREFERENCE_NAME, Context.MODE_PRIVATE);
		return settings.getBoolean(key, defaultValue);
	}

	public static void putObject(Context context, String tagName, Object obj) {
		try {
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			ObjectOutputStream oos = new ObjectOutputStream(baos);
			oos.writeObject(obj);
			String put = new String(
					Base64.encodeToString(baos.toByteArray(), 1));
			SharedPreferences mSharedPreferences = context
					.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
			Editor edit = mSharedPreferences.edit();
			edit.putString(tagName, put);
			edit.commit();
			baos.close();
			oos.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// public static void putObject(Context context, String sharedName,
	// String tagName, Object obj) {
	// try {
	// ByteArrayOutputStream baos = new ByteArrayOutputStream();
	// ObjectOutputStream oos = new ObjectOutputStream(baos);
	// oos.writeObject(obj);
	// String put = new String(
	// Base64.encodeToString(baos.toByteArray(), 1));
	// SharedPreferences mSharedPreferences = context
	// .getSharedPreferences(sharedName,
	// Context.MODE_PRIVATE);
	// Editor edit = mSharedPreferences.edit();
	// edit.putString(tagName, put);
	// edit.commit();
	// baos.close();
	// oos.close();
	// } catch (Exception e) {
	// e.printStackTrace();
	// }
	// }

	public static Object getObject(Context context, String tagName, Object obj) {
		try {
			SharedPreferences mSharedPreferences = context
					.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
			String personBase64 = mSharedPreferences.getString(tagName, "");
			byte[] base64Bytes = Base64.decode(personBase64.getBytes(), 1);
			ByteArrayInputStream bais = new ByteArrayInputStream(base64Bytes);
			ObjectInputStream ois = new ObjectInputStream(bais);
			obj = ois.readObject();
			bais.close();
			ois.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return obj;
	}

	// public static Object getObject(Context context, String sharedName,
	// String tagName, Object obj) {
	// try {
	// SharedPreferences mSharedPreferences = context
	// .getSharedPreferences(sharedName,
	// Context.MODE_PRIVATE);
	// String personBase64 = mSharedPreferences.getString(tagName, "");
	// byte[] base64Bytes = Base64.decode(personBase64.getBytes(), 1);
	// ByteArrayInputStream bais = new ByteArrayInputStream(base64Bytes);
	// ObjectInputStream ois = new ObjectInputStream(bais);
	// obj = ois.readObject();
	// } catch (Exception e) {
	// e.printStackTrace();
	// }
	// return obj;
	// }
	public static List<?> putList(Context context, String tagName, List<?> list) {
		SharedPreferences mSharedPreferences = context.getSharedPreferences(
				PREFERENCE_NAME, Context.MODE_PRIVATE);
		Editor edit = mSharedPreferences.edit();
		try {
			String liststr = ListUtils.SceneList2String(list);
			edit.putString(tagName, liststr);
			edit.commit();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return list;
	}

	public static List<?> getList(Context context, String tagName, List<?> list) {
		SharedPreferences sharedPreferences = context.getSharedPreferences(
				PREFERENCE_NAME, Context.MODE_PRIVATE);
		String liststr = sharedPreferences.getString(tagName, "");
		try {
			list = ListUtils.String2SceneList(liststr);
		} catch (StreamCorruptedException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return list;
	}
}
